var engine = getEngineList()[0];
var index = 0;
var helangSearch = {
    els:{},
    searchIndex:0,
    hot:{
        // 导航列表
        list: engine.list,
        // 搜索引擎
        href: getEngineList(),
    },
    /* 初始化 */
    init:function(){

        // 聚焦input
        document.getElementById("search-input").focus();

        // 判断是否手机端访问
        if(CONFIG.IS_CHECK_PHONE && isMobile()) {
            CONFIG.IS_USE_L2D = false;
        }

        // 搜索框上图像显示
        var logo = document.getElementById("logo");
        if(CONFIG.IS_USE_L2D) {
            // 使用看板娘: 初始化看板娘
            L2Dwidget.init(L2Dconfig);
            logo.style.backgroundImage = "url(img/loading.gif)";
        } else {
            // 使用GIF动图: 动图随机显示
            logo.style.backgroundImage = "url(img/logo/logo" + Math.floor(Math.random()*10) + ".gif)";
        }
        var _this = this;
        this.els={
            pickerBtn:$(".picker"),
            pickerList:$(".picker-list"),
            logo:$(".logo"),
            hotList:$(".hot-list"),
            input:$("#search-input"),
            button:$(".search")
        };

        // 设置热门搜索列表
        setEngineList();

        // 注册事件
        // 搜索类别选择按钮
        this.els.pickerBtn.click(function () {
            if(_this.els.pickerList.is(':hidden')) {
                setTimeout(function () {
                    _this.els.pickerList.show();
                },100);
            }
        });

        // 搜索类别选择列表
        this.els.pickerList.on("click",">li",function () {
            _this.els.logo.css("background-image",('url(img/logo/'+$(this).data("logo")+')'));
            _this.searchIndex=$(this).index();
            _this.els.pickerBtn.html($(this).html())
            setEngineList();
        });

        // 搜索 输入框 点击*/
        this.els.input.click(function () {
            setTimeout(function () {
                _this.els.hotList.show();
            },100);
        });

        var arr = [1, 2]; // 翻译/Js 不走快排

        // 搜索 输入框 输入
        this.els.input.on("input",function () {
            _this.els.hotList.show();
            index = -1;
            if($(this).val()){
                if(arr.indexOf(_this.searchIndex) !== -1) {
                    setEngineList();
                } else {
                    var engine = getEngineList()[_this.searchIndex];
                    // 搜索预览
                    var hotList = engine.list;
                    var searchList = [];
                    for(var i = 0; i < hotList.length; i++) {
                        var flag = getSimilarity($(this).val(), hotList[i].desc);
                        if(flag && hotList[i].type !== 0) {
                            // 设置匹配度排序权值
                            hotList[i].type = flag;
                            searchList.push(hotList[i]);
                        }
                    }
                    // 根据权值快排
                    searchList = quickSort(searchList);
                    if (!searchList.length) {
                        _this.els.hotList.hide();
                    }
                    setSearchList(searchList);
                }
            } else {
                setEngineList();
            }
        });

        // 文档
        $(document).click(function () {
            if(arr.indexOf(_this.searchIndex) === -1) {
                _this.els.pickerList.hide();
                _this.els.hotList.hide();
            }
        });

        // 搜索按钮
        document.getElementById('search').addEventListener('click', function() {
            // 导航
            if(helangSearch.searchIndex === 0) {
                window.location.href = engine.engine + dealSpecial(_this.els.input.val());
            }
            // 翻译
            if(helangSearch.searchIndex === 1) {
                window.location.href = fanyiUrl + dealSpecial(_this.els.input.val());
            }
        });
        $('#search-input').bind('keypress', function (event) {
            if(event.keyCode === 13) {
                window.location.href = engine.engine + dealSpecial(_this.els.input.val());
            }
        });
    }
};